<?php
	/*
	 * File: main_page.php
	 * Last modification: 20.07.2011
	 * Author(s): kivio.wanderley
	 * 
	 */
?>
	
<?php /** UPPER PHP CODE */
	
	$t_core_dir = dirname( __FILE__ ).DIRECTORY_SEPARATOR.'core'.DIRECTORY_SEPARATOR;
	require_once($t_core_dir . 'core_api.php');
	
	html_head_begin();
	core_require_js("jquery");
	core_require_js("jquery.validate");
	core_require_js("jquery.maskedinput");
	core_require_js("jquery.priceformat");
	core_require_js("jquery.datatables");
	
	core_require_css("css_default");
	html_head_end();
	
	html_begin();
	
	html_title_begin( "Main Page" );
	html_title_end();

	html_body_begin();

	# Start session
	if( ! isset($_SESSION) ) session_start();
?>

<?php /** MIDDLE PHP CODE */
	# Try to make the connection 
	if( !datab_is_connected() ) {
		config_get_global_array();
		datab_connect( config_get('host'), (int)config_get('port'), config_get('database'), 
					   config_get('user'), config_get('password') );
		config_unset();					   
	}
	
	# Print menu
	print_menu();

	# Add information box
	html_print_information_div();
	
	html_print_palette();

	# Page selected on top menu
	$page = gpc_get_string('page');
?>

<br /><br />

<div id="<?php echo $page ?>" style="display: none;">
<?php 
		# Avoid session fixation attack
		if (!isset($_SESSION['CREATED'])) {
    		$_SESSION['CREATED'] = time();
		} else if (time() - $_SESSION['CREATED'] > SESSION_REGENERATE_ID_SECS) {
    		//Session started more than 30 minates ago
    		session_regenerate_id(true);    //Change session ID for the current session an invalidate old session ID
    		$_SESSION['CREATED'] = time();  //Update creation time
		}
		
		if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > SESSION_EXPIRE_SECS)) {
		    # Last request was more than expire secs minates ago
		    session_destroy();   //Destroy session data in storage
		    session_unset();     //Unset $_SESSION variable for the runtime
		    print_header_redirect("login_page.php?erro=1003");
		}
		$_SESSION['LAST_ACTIVITY'] = time(); //Update last activity time stamp
		
		# Pages
		if($page == "logout_page") { 					
			session_destroy();
		    session_unset(); 
		    print_header_redirect("login_page.php");
		} elseif ($page == "home_page") {				require('home_page.php');
		} elseif ($page == "subscribe_page") {   require('subscribe_page.php');
		} elseif ($page == "update_page") {	require('update_page.php');  
		//MAIS PAGINAS ACRESCENTE elseif ($page == "MENUPAGINA") { require('ARQUIVOPAGINA.php');

		} elseif ($page == '' || $page == null) {		print_header_redirect( 'main_page.php?page=home_page' );
		} else {										set_info_msg("UNKNOWN_ERRO", I_ERR);
		}
		
	?>
</div>

<script>
$(document).ready(function () {
	$( "#<?php echo $page ?>" ).fadeIn(1500);
	$( "#information_msg" ).fadeOut(10000);
});
</script>
	
<?php /** BOTTOM PHP CODE */
	
	html_body_end();
	
	html_end();
?>	